package find;

import static util.Comparator.*;

/**
 * 递归就是利用方法栈 进行了后序遍历
 *
 * @author Liaorun
 */
public class FindMax {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            int[] array = generateRandomArray(10, 10, false);

            if (array.length == 0) {
                continue;
            }

            System.out.println("size" + array.length);
            int max = getMax(array);

            System.out.println("max" + max);
            printArray(array);

        }
    }

    private static int getMax(int[] array) {

        return process(array, 0, array.length - 1);
    }

    private static int process(int[] array, int leftPos, int rightPos) {
        // base case arr[L..R] 范围上只有一个数，直接返回
        if (leftPos == rightPos) {
            return array[leftPos];
        }
        // 获取中点
        int mid = leftPos + ((rightPos - leftPos) >> 1);
        int leftMax = process(array, leftPos, mid);
        int rightMax = process(array, mid + 1, rightPos);

        return Math.max(leftMax, rightMax);
    }
}
